#include "clock.h"
#include "ui_clock.h"
#include <QTimer>
#include <QTime>
#include <QPainter>
#include <QPen>
#include <QKeyEvent>

Clock::Clock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Clock)
{
    ui->setupUi(this);
    this->setFixedSize(500, 500);
    setWindowTitle(tr("时间"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

//    对称
    this->setStyleSheet("\
    background: qradialgradient(spread:repeat, \
    cx:0.5, cy:0.5, radius:0.077, fx:0.5, fy:0.5, \
    stop:0 #FFFACD, stop:0.497326 #FFE4B5, \
    stop:1 #FFFACD); \
    ");

//    //不对称
//    this->setStyleSheet("\
//    background: qradialgradient(spread:repeat, \
//    cx:0.8, cy:0.8, radius:0.077, fx:0.9, fy:0.9, \
//    stop:0 #FFFACD, stop:0.497326 #FFE4B5, \
//    stop:1 #FFFACD); \
//    ");

    //声明一个定时器
    QTimer *timer = new QTimer(this);

    //调用update()比调用的repaint()速度更快,闪烁更少。
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
//    connect(timer, SIGNAL(timeout()), this, SLOT(repaint()));

    //每1000毫秒刷新一次
    timer->start(1000);

    QWidget::installEventFilter(this);
}

void Clock::paintEvent(QPaintEvent *)
{
    //下面三个数组用来定义表针的四个顶点，以便后面的填充
    static const QPoint hourHand[4] =
    {
        QPoint(5, 0),
        QPoint(0, 14),
        QPoint(-5, 0),
        QPoint(0, -50)
    };
    static const QPoint minuteHand[4] =
    {
        QPoint(5, 0),
        QPoint(0, 14),
        QPoint(-5,0),
        QPoint(0, -70)
    };
    static const QPoint secondHand[4] =
    {
        QPoint(5, 0),
        QPoint(0, 14),
        QPoint(-5, 0),
        QPoint(0, -90)
    };

    //分别设定各表针所填充的颜色
    QColor hourColor(50, 200, 50, 240);
    QColor minuteColor(0, 127, 255, 191);
    QColor secondColor(200, 40, 50, 120);

    //绘制的范围 side = min(width(), height())
    int side = qMin(width(), height());

    //获取当前的时间
    QTime time = QTime::currentTime();

    //声明绘图工具
    QPainter painter(this);

    //消锯齿
    painter.setRenderHint(QPainter::Antialiasing);

    //重新定位坐标起始点为窗口的中心位置
    painter.translate(width() / 2, height() / 2 + 5);

    //设定花布的边界
    painter.scale(side / 240.0, side / 240.0);

//    //弧度渐变
////    QConicalGradient conicalGradient(width() / 2, height() / 2 + 5, 0);
//    QConicalGradient conicalGradient(0, 0, 0);
//    //创建了一个QConicalGradient对象实例，参数分别为中心坐标和初始角度
//    conicalGradient.setColorAt(0,Qt::green);
//    conicalGradient.setColorAt(0.2,Qt::white);
//    conicalGradient.setColorAt(0.4,Qt::blue);
//    conicalGradient.setColorAt(0.6,Qt::red);
//    conicalGradient.setColorAt(0.8,Qt::yellow);
//    conicalGradient.setColorAt(1.0,Qt::green);
//    //设置渐变的颜色和路径比例
//    painter.setBrush(QBrush(conicalGradient));
////    painter.drawEllipse(width() / 2, height() / 2 + 5, 240, 240);//在相应的坐标画出来
//    painter.drawEllipse(410,10,200,200);//在相应的坐标画出来

/*
 *  填充时针
*/
    painter.setPen(Qt::NoPen);
    //画刷颜色设定
    painter.setBrush(hourColor);
    //保存状态
    painter.save();
    //时针位置
    painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));//坐标轴旋转的角度
    //填充时针的区域
    painter.drawConvexPolygon(hourHand, 4);//根据三个坐标点绘图
    //恢复填充前的状态
    painter.restore();

    //绘制小时刻度线
    painter.setPen(Qt::green);
    painter.rotate(300.0);
    for (int i = 1; i <= 12; ++i)
    {
        //每次都是这样，先画跳线，再转个角
        QPen thickPen(Qt::green, 1.5);//设置刻度线为粗黑
        painter.setPen(thickPen);
        painter.drawLine(80, 0, 100, 0);
        painter.rotate(30.0);
    }

    //绘制小时数字
    QFont font("Helvetica");  //字体
    painter.setFont(font);
    font.setPixelSize(80);    //字大小
    for (int i = 1; i <= 12; ++i)
    {
        painter.translate(110,0);
        painter.rotate(360.0 - (i - 3) * 30.0);
        if (i == 10)
            painter.drawText(-10, -10, 20, 20, 0, QString("%1").arg(i));
        if ((i >= 11) && (i <= 12))
            painter.drawText(-8, -10, 20, 20, 0, QString("%1").arg(i));
        if ((i >= 1) && (i <= 7) && (i != 6) && (i != 3))
            painter.drawText(-3, -8, 20, 20, 0, QString("%1").arg(i));
        if (i == 3)
            painter.drawText(-3, -9, 20, 20, 0, QString("%1").arg(i));
        if (i == 6)
            painter.drawText(-5, -8, 20, 20, 0, QString("%1").arg(i));
        if (i == 8)
            painter.drawText(-5, -10, 20, 20, 0, QString("%1").arg(i));
        if (i == 9)
            painter.drawText(-5, -9, 20, 20, 0, QString("%1").arg(i));
        painter.rotate((i - 3) * 30.0);
        painter.translate(-110, 0);
        painter.rotate(30.0);
    }

    //填充分针
    painter.setPen(Qt::NoPen);
    painter.setBrush(minuteColor);
    painter.save();
    painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
    painter.drawConvexPolygon(minuteHand, 4);
    painter.restore();
    painter.setPen(minuteColor);
    for (int j = 0; j < 60; ++j)
    {
        if ((j % 5) != 0)
            painter.drawLine(90, 0, 100, 0);
        painter.rotate(6.0);
    }

    //填充秒针
    painter.setPen(Qt::NoPen);
    painter.setBrush(secondColor);
    painter.save();
    painter.rotate(6.0*time.second());
    painter.drawConvexPolygon(secondHand,4);
    painter.restore();
}

Clock::~Clock()
{
    delete ui;
}

bool Clock::eventFilter(QObject *target, QEvent *event)
{
    //键盘事件
    if (event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Escape)
        {
            this->hide();
            return true;
        }
    }
    if( target == this )
    {
       //窗口停用，变为不活动的窗口
        if(QEvent::WindowDeactivate == event->type())
        {
            this->hide();  //或者关闭窗口,加入自己的操作.
            return true ;
        }
        else
        {
            return false ;
        }
    }
    return QWidget::eventFilter(target, event);
}
